﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNA.Mvc
{
    /// <summary>
    /// Present the Observer base class.
    /// </summary>
    public abstract class EventObserver : IObserver<EventBase>, IDisposable
    {
        /// <summary>
        /// Process the event.
        /// </summary>
        /// <param name="value"></param>
        protected abstract void ProcessEvent(EventBase e);

        /// <summary>
        /// Trigger when event process error.
        /// </summary>
        /// <param name="error"></param>
        protected virtual void OnError(Exception error) { }

        /// <summary>
        /// Release the resources.
        /// </summary>
        protected virtual void Dispose() { }

        #region Implement the IObserver interface

        void IObserver<EventBase>.OnCompleted()
        {
        }

        void IObserver<EventBase>.OnError(Exception error)
        {
            this.OnError(error);
        }

        void IObserver<EventBase>.OnNext(EventBase value)
        {
            this.ProcessEvent(value);
        }

        void IDisposable.Dispose()
        {
            this.Dispose();
        }

        #endregion
    }

}
